package org.hs.phd.odi.tracking.tracker.particlefilter.tracker;

import java.awt.Rectangle;

import org.hs.phd.odi.tracking.tracker.Histogram;
import org.hs.phd.odi.tracking.tracker.TrackWindow;
import org.hs.phd.odi.tracking.tracker.particlefilter.core.Particle;
import org.hs.phd.odi.tracking.tracker.particlefilter.core.ParticleFilter;
import org.hs.phd.odi.tracking.tracker.particlefilter.observation.HistogramMatchingObservationModel;
import org.hs.phd.odi.tracking.tracker.particlefilter.resampling.ImportanceResamplingStrategy;
import org.hs.phd.odi.tracking.tracker.particlefilter.state.RotatedRectangleParticleState;
import org.hs.phd.odi.tracking.tracker.particlefilter.transition.RectangleAutoRegressiveDynamicsTransitionModel;
import org.hs.phd.odi.tracking.tracker.trackwindow.RegularGridRectangularTrackWindow;

import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class HistogramMatchingBasedParticleFilterTracker extends AbstractSingleObjectParticleFilterTracker<RotatedRectangleParticleState> {

	private static final int NUMBER_OF_PARTICLES = 100;

	public HistogramMatchingBasedParticleFilterTracker(){
		super( new RegularGridRectangularTrackWindow() );
	}
	

	@Override
	protected Particle<RotatedRectangleParticleState> createInitialParticle(TrackWindow trackWindow) {
		Rectangle trackRect = trackWindow.getAsPolygon().getBounds();
		RotatedRectangleParticleState initialState = 
				new RotatedRectangleParticleState(trackRect);
		
		return new Particle<RotatedRectangleParticleState>(1, initialState);
	}

	@Override
	protected ParticleFilter<RotatedRectangleParticleState> createParticleFilter(IplImage initialFrame, Histogram targetHistogram, TrackWindow trackWindow) {
		return new ParticleFilter<RotatedRectangleParticleState>(NUMBER_OF_PARTICLES, 
				//new NULLTransitionModel(),
				new RectangleAutoRegressiveDynamicsTransitionModel(initialFrame.width(), initialFrame.height()),
				new HistogramMatchingObservationModel<RotatedRectangleParticleState>(targetHistogram, trackWindow, this),
				new ImportanceResamplingStrategy<RotatedRectangleParticleState>());
	}


	@Override
	public void updateTrackWindowUsingParticleState(TrackWindow trackWindow, RotatedRectangleParticleState particleState) {
		trackWindow.moveToBestMatchingLocationFor(particleState.getShape());
	}

	@Override
	public String getName() {
		return "Histogram Based Particle Filter Tracker";
	}


}
